﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.IO;
using DBAccessLib;

namespace LineWatchSimple.WinForm
{
    public partial class ScreenSaver : Form
    {
        public ProductManagementView ProductManagementForm;
        private DataTable dtCommentInfo = new DataTable();
        private string commentInfo = string.Empty;
        private int currentImageIndex = 0;

        public ScreenSaver()
        {
            InitializeComponent();
            this.timer.Enabled = false;
            this.SetFont(this.label1, "Microsoft YaHei", ConfigInfo.FontSizeScreenSaver, FontStyle.Bold);
#if DEBUG
            this.TopMost = false;
#else
            this.TopMost = true;
#endif
        }

        /// <summary>
        /// Set Font
        /// </summary>
        /// <param name="control"></param>
        /// <param name="familyName"></param>
        /// <param name="emSize"></param>
        /// <param name="style"></param>
        private void SetFont(Control control, string familyName, float emSize, FontStyle style)
        {
            control.Font = new System.Drawing.Font(familyName,
                                                   emSize,
                                                   style,
                                                   System.Drawing.GraphicsUnit.Point,
                                                   ((byte)(0))
                                                   );
        }

        /// <summary>
        /// Get Pictures
        /// </summary>
        private void GetPictures()
        {
            this.imageList.Images.Clear();

            string fileDir = AppDomain.CurrentDomain.BaseDirectory
                                + ConfigurationManager.AppSettings["PictureFileDirectory"];

            DirectoryInfo dir = new DirectoryInfo(fileDir);

            FileInfo[] file = dir.GetFiles();

            if ((file != null)
                && (file.Length > 0))
            {
                for (int i = 0; i < file.Length; i++)
                {
                    if (file[i].Name.ToUpper().EndsWith(".JPG"))
                    {
                        this.InitImageList(file[i].Name);
                    }
                }
            }
        }

        /// <summary>
        /// Init Image List
        /// </summary>
        /// <param name="lineCode"></param>
        /// <param name="fileName"></param>
        private void InitImageList(string fileName)
        {
            if (!string.IsNullOrWhiteSpace(fileName))
            {
                string filePath = AppDomain.CurrentDomain.BaseDirectory
                                + ConfigurationManager.AppSettings["PictureFileDirectory"]
                                + "/"
                                + fileName;

                FileInfo file = new FileInfo(filePath);
                if (file.Exists)
                {
                    Image image = Image.FromFile(file.FullName);
                    this.imageList.Images.Add(image);
                }
            }
        }

        /// <summary>
        /// Show Picture
        /// </summary>
        private void ShowPicture()
        {
            if (this.currentImageIndex < this.imageList.Images.Count - 1)
            {
                this.currentImageIndex++;
            }
            else
            {
                this.currentImageIndex = 0;
                this.Close();
            }

            if (this.imageList.Images.Count > 0)
            {
                this.pictureBox.Image = this.imageList.Images[this.currentImageIndex];
            }

            this.pictureBox.Update();
            this.pictureBox.Refresh();

            this.Update();
            this.Refresh();
        }

        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
            this.Close();
        }

        private void label1_MouseClick(object sender, MouseEventArgs e)
        {
            this.Close();
        }

        private void pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            this.Close();
        }

        private void pictureBox_MouseClick(object sender, MouseEventArgs e)
        {
            this.Close();
        }

        private void pictureBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            this.Close();
        }

        private void ScreenSaver_KeyDown(object sender, KeyEventArgs e)
        {
            this.Close();
        }

        private void ScreenSaver_KeyPress(object sender, KeyPressEventArgs e)
        {
            this.Close();
        }

        private void ScreenSaver_MouseClick(object sender, MouseEventArgs e)
        {
            this.Close();
        }

        private void ScreenSaver_MouseDown(object sender, MouseEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Form Closing
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ScreenSaver_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.imageList.Images.Clear();
            this.ProductManagementForm.SetScreenSaverTimerEnable(true);
        }

        /// <summary>
        /// Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ScreenSaver_Load(object sender, EventArgs e)
        {
            this.imageList.Images.Clear();
            this.dtCommentInfo = LineInfo.GetCommentInfo();

            if (this.dtCommentInfo.Rows.Count <= 0)
            {
                this.GetPictures();
            }
            else
            {
                this.commentInfo = this.dtCommentInfo.Rows[0]["Comment"].ToString();
            }

            this.currentImageIndex = 0;

            if (this.imageList.Images.Count > 0)
            {
                this.pictureBox.Image = this.imageList.Images[this.currentImageIndex];

                this.timer.Interval = ConfigInfo.RefreshSaverSpan / this.imageList.Images.Count;
            }
            else
            {
                this.timer.Interval = ConfigInfo.RefreshSaverSpan;
            }
        }

        /// <summary>
        /// Shown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ScreenSaver_Shown(object sender, EventArgs e)
        {
            this.currentImageIndex = 0;

            if (this.imageList.Images.Count > 0)
            {
                this.pictureBox.Image = this.imageList.Images[this.currentImageIndex];

                this.label1.Visible = false;
                this.pictureBox.Visible = true;
                
                this.tableLayoutPanel1.RowStyles.Clear();
                this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            }
            else
            {
                this.label1.Text = this.commentInfo;

                this.label1.Visible = true;
                this.pictureBox.Visible = false;

                this.tableLayoutPanel1.RowStyles.Clear();
                this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
                this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
            }
            
            this.timer.Enabled = true;
        }

        /// <summary>
        /// timer Tick
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer_Tick(object sender, EventArgs e)
        {
            this.ShowPicture();
        }
    }
}
